package com.test.mchelepesa.activity_fragment_rice.fragment_rice;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.mchelepesa.R;
import com.test.mchelepesa.activity_fragment_rice.PermissionActivity_rice;
import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseLoanFragment_rice;
import com.test.mchelepesa.activity_fragment_rice.loan_rice.LoanMainActivity_rice;
import com.test.mchelepesa.bean_rice.StaticConfig_rice;
import com.test.mchelepesa.bean_rice.user_info_rice.LoanLimitInfo_rice;
import com.test.mchelepesa.dialog_rice.CustomerServiceDialog_rice;
import com.test.mchelepesa.util_rice.PermissionUtil_rice;
import com.test.mchelepesa.util_rice.StringUtil_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;
import com.test.mchelepesa.util_rice.analytics_rice.util_rice.AnalyticsUtil_rice;


public class LoanMainFragment_rice extends BaseLoanFragment_rice {

    private TextView loanAmountTv;

    private TextView limit1Tv;
    private TextView limit2Tv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_loan_main_rice, container, false);
        init_rice(view);
        return view;
    }

    @SuppressLint("SetTextI18n")
    private void init_rice(View view) {

        loanAmountTv = view.findViewById(R.id.fragment_loan_main_tv_loan_amount);

        limit1Tv = view.findViewById(R.id.fragment_loan_main_tv_limit_1);
        limit2Tv = view.findViewById(R.id.fragment_loan_main_tv_limit_2);

        initLicense(
                view.findViewById(R.id.fragment_loan_main_view_license),
                view.findViewById(R.id.fragment_loan_main_license_logo),
                view.findViewById(R.id.fragment_loan_main_license_company_name)
        );

        view.findViewById(R.id.fragment_loan_main_view_limit).setOnClickListener(v -> Toast.makeText(requireContext(),getString(R.string.home_tips),Toast.LENGTH_SHORT).show());

        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.fragment_loan_main_refresh_layout);
        refreshLayout.setOnRefreshListener(() -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint((this.getClass().getSimpleName() + "_REFRESH_PULL").toUpperCase());
            refreshLayout.setRefreshing(false);
            HttpManager_rice.getInstance().requestUserProcess();
        });

        final Runnable afterPermission = () -> {

            if (!PermissionUtil_rice.checkLaunchPermissions(requireContext(),Manifest.permission.READ_SMS,Manifest.permission.CAMERA)){
                startActivity(new Intent(requireContext(), PermissionActivity_rice.class));
                return;
            }

            try {
                AnalyticsUtil_rice.INSTANCE.saveDeviceS2SInfo();
            } catch (Exception e) {
                StringUtil_rice.printException(e);
            }

            HttpManager_rice.getInstance().requestUserProcess();
        };

        view.findViewById(R.id.fragment_loan_main_layout_apply_btn).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPointAndUpload((this.getClass().getSimpleName() + "_APPLY_CLICK").toUpperCase());
            LoanMainActivity_rice.processFlag = true;
            afterPermission.run();
        });

        view.findViewById(R.id.fragment_loan_main_layout_apply_btn_2).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPointAndUpload((this.getClass().getSimpleName() + "_APPLY_2_CLICK").toUpperCase());
            LoanMainActivity_rice.processFlag = true;
            afterPermission.run();
        });

        view.findViewById(R.id.fragment_loan_main_ib_customer_service).setOnClickListener(v -> new CustomerServiceDialog_rice(requireContext()).show());

        HttpManager_rice.getInstance().queryLoanLimit();

    }

    @SuppressLint("SetTextI18n")
    public void updateLoanAmount_rice() {
        String currency = LoanLimitInfo_rice.getInstance().getCurrency();
        loanAmountTv.setText(StringUtil_rice.getSafeString(StaticConfig_rice.getMoneyUnit(currency)).toUpperCase() + " "
                + LoanLimitInfo_rice.getInstance().getLimitAmount());
        limit1Tv.setText(StringUtil_rice.getSafeString(StaticConfig_rice.getMoneyUnit(currency)).toUpperCase() + " "
                + LoanLimitInfo_rice.getInstance().getLimitIncreasedAmount1());
        limit2Tv.setText(StringUtil_rice.getSafeString(StaticConfig_rice.getMoneyUnit(currency)).toUpperCase() + " "
                + LoanLimitInfo_rice.getInstance().getLimitIncreasedAmount2());
    }
}